package com.tiocloud.chat.feature.home.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.tiocloud.account.TioAccount;
import com.tiocloud.chat.R;
import com.tiocloud.chat.constant.TioConfig;
import com.tiocloud.chat.databinding.TioUserFragmentBinding;
import com.tiocloud.chat.feature.curr.detail.CurrDetailActivity;
import com.tiocloud.chat.feature.home.user.mvp.UserContract;
import com.tiocloud.chat.feature.home.user.mvp.UserPresenter;
import com.tiocloud.chat.feature.settings.SettingsActivity;
import com.tiocloud.chat.util.StringUtil;
import com.watayouxiang.androidutils.page.TioFragment;
import com.watayouxiang.androidutils.util.ClickUtils;
import com.watayouxiang.db.prefernces.TioDBPreferences;
import com.watayouxiang.httpclient.model.response.UserCurrResp;
import com.watayouxiang.qrcode.TioQRCode;
import com.watayouxiang.qrcode.feature.qrcode_my.MyQRCodeActivity;
import com.watayouxiang.wallet.TioWallet;
import com.watayouxiang.wallet.feature.wallet.WalletActivity;

/**
 * author : TaoWang
 * date : 2020-02-03
 * desc :
 */
public class UserFragment extends TioFragment implements UserContract.View {

    private UserPresenter presenter;
    private TioUserFragmentBinding binding;

    @Override
    public void initUI() {
        setStatusBarCustom(binding.flStatusBar);
        binding.tvName.setText("");
        binding.tvEmail.setText("");
        binding.rlSettings.setOnClickListener(v -> SettingsActivity.start(getActivity()));
        binding.rlModifyInfo.setOnClickListener(v -> CurrDetailActivity.start(getActivity()));
        binding.hivAvatar.setOnClickListener(v -> CurrDetailActivity.start(getActivity()));
        if (TioConfig.IS_DEVELOPE && TioWallet.IS_OPEN) {
            binding.rlWallet.setVisibility(View.VISIBLE);
            binding.rlWallet.setOnClickListener(v -> {
                if (ClickUtils.isViewSingleClick(v)){
                    WalletActivity.start(getActivity());
                }
            });
        } else {
            binding.rlWallet.setVisibility(View.GONE);
        }
        if (TioQRCode.IS_OPEN) {
            binding.ivQrcode.setVisibility(View.VISIBLE);
            binding.ivQrcode.setOnClickListener(v -> {
                if (ClickUtils.isViewSingleClick(v))
                    MyQRCodeActivity.start(getActivity());
            });
        } else {
            binding.ivQrcode.setVisibility(View.GONE);
        }
    }

    @Override
    public void updateUI(UserCurrResp resp) {
        binding.tvName.setText(StringUtil.nonNull(resp.nick));
        binding.tvEmail.setText(StringUtil.nonNull(resp.phone));
        binding.hivAvatar.tio_roundAvatar(resp.avatar);
        // 检测是否绑定手机
        TioAccount.checkIsBindPhone(resp);
        // 存储当前uid
        TioDBPreferences.saveCurrUid(resp.id);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.tio_user_fragment, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new UserPresenter(this);
        presenter.init();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.detachView();
    }

    public void onRefresh() {
        if (presenter != null) {
            presenter.updateUIData();
        }
    }
}
